import json
from flask import Blueprint
from flask import request, Response
from src.common.logger import getLogger
from src.modules.home.service import user_service

logger = getLogger()
api = Blueprint('user', __name__)

@api.route('/user/page', methods = ['GET'])
def user_page():
    args = request.args
    logger.info(f"user_api user_page args: {args}")
    page = user_service.load_user_page(args)
    return Response(json.dumps({ "code": 200, "data": page }), status = 200, mimetype = "application/json")

@api.route('/user/add', methods = ['POST'])
def add_user():
    args = request.form
    logger.info(f"user_api add_user args: {args}")
    user_service.add_system_user(args)
    page = user_service.load_user_page(args)
    return Response(json.dumps({ "code": 200, "data": page }), status = 200, mimetype = "application/json")

@api.route('/user/update', methods = ['POST'])
def update_user():
    args = request.form
    logger.info(f"user_api update_user args: {args}")
    user_service.update_system_user(args)
    page = user_service.load_user_page(args)
    return Response(json.dumps({ "code": 200, "data": page }), status = 200, mimetype = "application/json")

@api.route('/user/delete', methods = ['POST'])
def delete_user():
    args = request.form
    logger.info(f"user_api delete_user args: {args}")
    user_service.delete_system_user(args)
    page = user_service.load_user_page(args)
    return Response(json.dumps({ "code": 200, "data": page }), status = 200, mimetype = "application/json")

@api.route('/user/select', methods = ['GET'])
def select_user():
    args = request.args
    logger.info(f"user_api select_user args: {args}")
    user = user_service.select_system_user(args)
    return Response(json.dumps({ "code": 200, "data": user }), status = 200, mimetype = "application/json")
